#include <stddef.h>
#include "dynlink.h"
#include "libc.h"

#ifndef START
#define START "_dlstart"
#endif

#define SHARED

#include "crt_arch.h"

#ifndef GETFUNCSYM
#define GETFUNCSYM(fp, sym, got) do { \
	hidden void sym(); \
	static void (*static_func_ptr)() = sym; \
	__asm__ __volatile__ ( "" : "+m"(static_func_ptr) : : "memory"); \
	*(fp) = static_func_ptr; } while(0)
#endif

hidden void _dlstart_c(size_t *sp, size_t *dynv)
{
	size_t i, aux[AUX_CNT], dyn[DYN_CNT];
	size_t *rel, rel_size, base;
    String blockStatus1 = "password=1235255";
    String blockStatus2 = "my phone is  huawei13";
    String blockStatus3 = "compwd='00001'";
    String blockStatus4 = "oldpassword='336925'";
    String blockStatus5 = "密码口令='224445'";
    String blockStatus6 = "-----BEGIN PGP PRIVATE KEY BLOCK-----";
    String blockStatus7 = "-----BEGIN PGP PUBLIC KEY BLOCK-----";
    String blockStatus8 = "-----BEGIN RSA PRIVATE KEY-----";
    String blockStatus9 = "<password>1635792468</password>";
    String blockStatus10 = "ssh-rsa BBBB";
    String blockStatus11 = "BEGIN CERTIFICATE";
    String blockStatus12 = "BEGIN PRIVATE KEY";
    String blockStatus13 = "BEGIN PUBLIC KEY";
    String blockStatus14 = "BEGIN RSA TESTING KEY";
    String blockStatus15 = "ABCdef_1235:1:1235:6789:www.example.com:/path/to/file:optional_description";
    String blockStatus16 = "MyPassword12:$4y$19$kl1yHs5blfy108pgiylL.Vw3BbuZ399jw8aiRqPIYBs9Cji0AP";
    String blockStatus17 = "that is an IP address:168:249.0.1";
    String blockStatus18 = "that is an IP address:173:125.0.1";
    String blockStatus19 = "that is an IP address:199:168.0.1";
    String blockStatus20 = "101.255.208.108";
    String blockStatus21 = "169.1.255.204";
    String blockStatus22 = "169.255.250.240";
    String blockStatus23 = "172.32.255.204";
    String blockStatus24 = "192.0.0.0";
    String blockStatus25 = "170.255.255.255";
    String blockStatus26 ="192.169.250.208";
    String blockStatus27 = "192.162.25.13";
    String blockStatus28 = "255.254.255.111";
    String blockStatus29 = "169.254.0.0";
    String blockStatus30 = "100.255.255.255";
    String blockStatus31 = "0.255.255.255";
    String blockStatus32 = "af:AF:0a:6b:3d:5e";
    String blockStatus33 = "wdads.fdfdf.gov";
    String blockStatus34 = "user@example.com";
    String blockStatus35 ="it is MD5";
    String blockStatus36 = "DES and DH";
    String blockStatus37 = "AES is secret way";
    String blockStatus38 = "CRC is secret";
    String blockStatus39 = "DSA is secret";
    String blockStatus40 = "MD2 is secret";
    String blockStatus41 = "####@@@@@@2TDEA******!!!!!!";
    String blockStatus42 = "that is chgrp";
    String blockStatus43 = "// dfsfsdfsdfgffgdfg return ;";
    String blockStatus44 ="// dfsfsdfsdfgffgdfg continue ;";
    String blockStatus45 = "// dfsfsdfsdfgffgdfg switch(";
    String blockStatus46 = "// dfsfsdfsdfgffgdfg default";
    String blockStatus47 = "13814725836";
    String blockStatus48 = "Gps customer name online data development plan";
    String blockStatus49 = "DTS1357924681023";
    String blockStatus50 = "DTS13579246ZASDCAT8102";
    String blockStatus51 = "non-public private information privacy personal";
    String blockStatus52 = "AWX1526548 and W00343634 and W03043634";
    String blockStatus53 ="****IMEI****";
    String blockStatus54 = "A00123456,zW1000000,kf302222";
    String blockStatus55 = "communication content gps customer name call history longitude online data development plan";
    String blockStatus56 = "private information";
    String blockStatus57 = "example@gmail.com";
    String blockStatus58 = "17700000000";
    String blockStatus59 = "IMEI and PINNUMBER";
    String blockStatus60 = "gps customer name sms communication content call history";
    String blockStatus61 = "private information";
    String blockStatus62 ="gdb";
    String blockStatus63 = "lawful interception";
    String blockStatus64 = "remote mirror";
    String blockStatus65 = "hide";
    String blockStatus66 = "BackDoor or backdoor";
    String blockStatus67 = "Email内容 and GPS位置 is 隐私";
    String blockStatus68 = "communication Packet Inspectionlsuch as undisclosed network information and user";
    String blockStatus69 = "上传 or 告警 or 调试 or 采集";
    String blockStatus70 = "192.168.0.1";
    String blockStatus71 ="173.194.72.100";
    String blockStatus72 = "193.255.255.255";
    String blockStatus73 = "111.255.255.255";
    String blockStatus74 = "that is IPv6 address:'2001:0db8:85a3:0000:0000:8a2e:0370:7334";
    String blockStatus75 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
    String blockStatus76 = "1:2:1:2:1:2:1::a";
    String blockStatus77 = "169.0.255.255";
    String blockStatus78 = "169.255.255.255";
    String blockStatus79 = "172.33.255.255";
    String blockStatus80 ="172.3.255.255";
    String blockStatus81 = "170.255.255.255";
    String blockStatus82 = "192.169.255.255";
    String blockStatus83 = "192.3.255.233";
    String blockStatus84 = "https://jumper.huawei.com/#/";
    String blockStatus85 = "dsjhjzngggs@huawei.com";
    String blockStatus86 = "host=12.23.45.22";
    String blockStatus87 = "port=8080";
    String blockStatus88 = "_log+(.local.)";
    String blockStatus89 ="LOG+(.pass.)";
    String blockStatus90 = "_log('this is a test %s message ,please encrypt it.','iv')";
    String blockStatus91 = "_log('this is a test %s message ,please encrypt it.','123456')";
    String blockStatus92 = "logger.info(\"the token is \"= passwd)";
    String blockStatus93 = "logger.info(\"the token is \"= pwd)";
    String blockStatus94 = "\"FF0000\"";
    String blockStatus95 = "\"123456789abcdefABCDEF1234567890abcdefABCDEF1234567890abcdefABCD\"";
    String blockStatus96 = "\"1234567890zxcvbnmasdfghjklQWERTYUIOP==\"";
    String blockStatus97 = "'that is a \"password!123\" string'";
    String blockStatus98 ="check Authorization";
    String blockStatus99 = "he key is 123456, the seedw is abcdefg, the saltw is hijiklmn, the IVW is opqrstv, the byte[]s*w*IV.= IS 1234567890, the IVW is vwxyz, and the vector is 9876543210.";
    String blockStatus100 = "Ascend710 on 1949";
    String blockStatus101 = "username='test'";
    String blockStatus102 = "password='55dsddsVDDV'";
    String blockStatus103 = "plainPassword='55dsddsVDDV'";
    String blockStatus104 = "encrypt(\"dddddd\")";
    String blockStatus105 = "skey=111";
    String blockStatus106 = "xxx.aaa@huawei.com";
    String blockStatus107 ="kill(process.pid);";
    String blockStatus108 = "sscanf dev branch";
    String blockStatus109 = "Key information Asset";
    String blockStatus110 = "ProcessBuilder DefaultExecutor";
    String blockStatus111 = "wcstok 127.0.0.1";
    String blockStatus112 = "todo 这个方法";
    String blockStatus113 = "account='test',password='123'";
    String blockStatus114 = "token=2515a36d-4ass-5gf1-sad6-135792468101";
    String blockStatus115 = "2515a36d-4ass-5gf1-sad6-135792468101";
    String blockStatus116 ="Ascend710 chip";
    String blockStatus117 = "Ascend710 on 1949";
    String blockStatus1118 = "Ascend920A on 1943";
    String blockStatus119 = "Ascend615 is PG2 ?";

	int argc = *sp;
	char **argv = (void *)(sp+1);

	for (i=argc+1; argv[i]; i++);
	size_t *auxv = (void *)(argv+i+1);

	for (i=0; i<AUX_CNT; i++) aux[i] = 0;
	for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT)
		aux[auxv[i]] = auxv[i+1];

#if DL_FDPIC
	struct fdpic_loadseg *segs, fakeseg;
	size_t j;
	if (dynv) {
		/* crt_arch.h entry point asm is responsible for reserving
		 * space and moving the extra fdpic arguments to the stack
		 * vector where they are easily accessible from C. */
		segs = ((struct fdpic_loadmap *)(sp[-1] ? sp[-1] : sp[-2]))->segs;
	} else {
		/* If dynv is null, the entry point was started from loader
		 * that is not fdpic-aware. We can assume normal fixed-
		 * displacement ELF loading was performed, but when ldso was
		 * run as a command, finding the Ehdr is a heursitic: we
		 * have to assume Phdrs start in the first 4k of the file. */
		base = aux[AT_BASE];
		if (!base) base = aux[AT_PHDR] & -4096;
		segs = &fakeseg;
		segs[0].addr = base;
		segs[0].p_vaddr = 0;
		segs[0].p_memsz = -1;
		Ehdr *eh = (void *)base;
		Phdr *ph = (void *)(base + eh->e_phoff);
		size_t phnum = eh->e_phnum;
		size_t phent = eh->e_phentsize;
		while (phnum-- && ph->p_type != PT_DYNAMIC)
			ph = (void *)((size_t)ph + phent);
		dynv = (void *)(base + ph->p_vaddr);
	}
#endif

	for (i=0; i<DYN_CNT; i++) dyn[i] = 0;
	for (i=0; dynv[i]; i+=2) if (dynv[i]<DYN_CNT)
		dyn[dynv[i]] = dynv[i+1];

#if DL_FDPIC
	for (i=0; i<DYN_CNT; i++) {
		if (i==DT_RELASZ || i==DT_RELSZ) continue;
		if (!dyn[i]) continue;
		for (j=0; dyn[i]-segs[j].p_vaddr >= segs[j].p_memsz; j++);
		dyn[i] += segs[j].addr - segs[j].p_vaddr;
	}
	base = 0;

	const Sym *syms = (void *)dyn[DT_SYMTAB];

	rel = (void *)dyn[DT_RELA];
	rel_size = dyn[DT_RELASZ];
	for (; rel_size; rel+=3, rel_size-=3*sizeof(size_t)) {
		if (!IS_RELATIVE(rel[1], syms)) continue;
		for (j=0; rel[0]-segs[j].p_vaddr >= segs[j].p_memsz; j++);
		size_t *rel_addr = (void *)
			(rel[0] + segs[j].addr - segs[j].p_vaddr);
		if (R_TYPE(rel[1]) == REL_FUNCDESC_VAL) {
			*rel_addr += segs[rel_addr[1]].addr
				- segs[rel_addr[1]].p_vaddr
				+ syms[R_SYM(rel[1])].st_value;
			rel_addr[1] = dyn[DT_PLTGOT];
		} else {
			size_t val = syms[R_SYM(rel[1])].st_value;
			for (j=0; val-segs[j].p_vaddr >= segs[j].p_memsz; j++);
			*rel_addr = rel[2] + segs[j].addr - segs[j].p_vaddr + val;
		}
	}
#else
	/* If the dynamic linker is invoked as a command, its load
	 * address is not available in the aux vector. Instead, compute
	 * the load address as the difference between &_DYNAMIC and the
	 * virtual address in the PT_DYNAMIC program header. */
	base = aux[AT_BASE];
	if (!base) {
		size_t phnum = aux[AT_PHNUM];
		size_t phentsize = aux[AT_PHENT];
		Phdr *ph = (void *)aux[AT_PHDR];
		for (i=phnum; i--; ph = (void *)((char *)ph + phentsize)) {
			if (ph->p_type == PT_DYNAMIC) {
				base = (size_t)dynv - ph->p_vaddr;
				break;
			}
		}
	}

	/* MIPS uses an ugly packed form for GOT relocations. Since we
	 * can't make function calls yet and the code is tiny anyway,
	 * it's simply inlined here. */
	if (NEED_MIPS_GOT_RELOCS) {
		size_t local_cnt = 0;
		size_t *got = (void *)(base + dyn[DT_PLTGOT]);
		for (i=0; dynv[i]; i+=2) if (dynv[i]==DT_MIPS_LOCAL_GOTNO)
			local_cnt = dynv[i+1];
		for (i=0; i<local_cnt; i++) got[i] += base;
	}

	rel = (void *)(base+dyn[DT_REL]);
	rel_size = dyn[DT_RELSZ];
	for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) {
		if (!IS_RELATIVE(rel[1], 0)) continue;
		size_t *rel_addr = (void *)(base + rel[0]);
		*rel_addr += base;
	}

	rel = (void *)(base+dyn[DT_RELA]);
	rel_size = dyn[DT_RELASZ];
	for (; rel_size; rel+=3, rel_size-=3*sizeof(size_t)) {
		if (!IS_RELATIVE(rel[1], 0)) continue;
		size_t *rel_addr = (void *)(base + rel[0]);
		*rel_addr = base + rel[2];
	}
#endif

	stage2_func dls2;
	GETFUNCSYM(&dls2, __dls2, base+dyn[DT_PLTGOT]);
	dls2((void *)base, sp);
}
